<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class MainController extends AdminbaseController {
	
    public function index(){
    	
    	$mysql= M()->query("select VERSION() as version");
    	$mysql=$mysql[0]['version'];
    	$mysql=empty($mysql)?L('UNKNOWN'):$mysql;
        //接收商户ID
        $shop_id = I('shop_id');

    	//server infomaions
    	$info = array(
    			L('OPERATING_SYSTEM') => PHP_OS,
    			L('OPERATING_ENVIRONMENT') => $_SERVER["SERVER_SOFTWARE"],
    			L('PHP_RUN_MODE') => php_sapi_name(),
    			L('MYSQL_VERSION') =>$mysql,
    			L('PROGRAM_VERSION') => SIMPLEWIND_CMF_VERSION . "&nbsp;&nbsp;&nbsp; [<a href='http://www.thinkcmf.com' target='_blank'>ThinkCMF</a>]",
    			L('UPLOAD_MAX_FILESIZE') => ini_get('upload_max_filesize'),
    			L('MAX_EXECUTION_TIME') => ini_get('max_execution_time') . "s",
    			L('DISK_FREE_SPACE') => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
    	);
        
        //========================
        //获取注册人数
        $model = M('');
        $formget = I('formget');
        $user_start_time= ($formget["user_start_time"]);//查询时间  注册用户
        $user_end_time= ($formget["user_end_time"]);
        $user_end_time2 =strtotime($user_end_time)+ 86400;
        $user_end_time2 = date("Y-m-d ", $user_end_time2);
        //dump($user_time);
  
        if(!empty($user_start_time) && !empty($user_end_time))
        {
          $sql = "SELECT DATE_FORMAT(regist_time, '%Y-%m-%d %H') time_hour,count(user_id) FROM db_app_users
                  WHERE unix_timestamp(regist_time) >= unix_timestamp('$user_start_time')
                  AND unix_timestamp(regist_time) <= unix_timestamp('$user_end_time2')
                  GROUP BY
	          DATE_FORMAT(regist_time, '%H');";
        }elseif(empty($user_end_time) && !empty($user_start_time)){
            $sql = "select DATE_FORMAT(regist_time, '%Y-%m-%d %H') time_hour,count(user_id) from db_app_users  
               where DATE_FORMAT(regist_time, '%Y-%m-%d') = date_format('$user_start_time', '%Y-%m-%d') 
               GROUP BY DATE_FORMAT(regist_time, '%Y-%m-%d %H');";
        }elseif(!empty($user_end_time) && empty($user_start_time))
        {
            $sql = "select DATE_FORMAT(regist_time, '%Y-%m-%d %H') time_hour,count(user_id) from db_app_users  
               where DATE_FORMAT(regist_time, '%Y-%m-%d') = date_format('$user_end_time', '%Y-%m-%d') 
               GROUP BY DATE_FORMAT(regist_time, '%Y-%m-%d %H');";
        }
        elseif(empty($user_end_time) && empty($user_start_time))
        {
           $sql = "select DATE_FORMAT(regist_time, '%Y-%m-%d %H') time_hour,count(user_id) from db_app_users  
               where DATE_FORMAT(regist_time, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d') 
               GROUP BY DATE_FORMAT(regist_time, '%Y-%m-%d %H');"; 
        }
        
       // $sql ="select DATE_FORMAT(regist_time, '%Y-%m-%d %H') time_hour,count(user_id) from db_app_users".$where;  
              
        $user = $model->query($sql);
//        echo $model->getLastSql();
//        dump($user);

        if($user)
        {
            foreach($user as $k => $v)
            {
                $user2[$k] = substr($v["time_hour"],-2);
                $user3[$user2[$k]] = $v["count(user_id)"];          
              }   
            for($i=1;$i<=24;$i++)
            {
                foreach ($user3 as $k => $v) {
                  //static $month;
                    if($i == $k)
                    {
                      $month[$i] = (int)$user3[$k]; 
                      break;
                    } else {
                       $month[$i]=0;
                    }
                }          
            }
          $users  =  implode ( "," ,  $month );
          $users = "[".$users."]";
        }else
        {
            $users ="[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]";
        }

        $AppUsers = D('AppUsers');
        $user_id = array();
        $where = "";
//        dump($AppUsers->get_user_id_from_shop_ids(10));
//
//        exit;
        if(!empty($shop_id)){
            $user_id = $AppUsers->get_user_id_from_shop_ids($shop_id);
            $where = "AND user_id in (".$user_id.") ";
        }


        $order_start_time= $formget["order_start_time"];  //查询时间 订单
        $order_end_time= $formget["order_end_time"];//1484236800
//        dump($order_end_time);
        $order_end_time2 =strtotime($order_end_time)+ 86400;
        $order_end_time2 = date("Y-m-d ", $order_end_time2);
        //获取订单
        if(!empty($order_start_time) && !empty($order_end_time) && !empty($shop_id))
        {

           $sql2 ="select DATE_FORMAT(order_time, '%Y-%m-%d %H') time_hour,count(order_id) from db_order  
                  WHERE unix_timestamp(order_time) >= unix_timestamp('$order_start_time')
                  AND unix_timestamp(order_time) <= unix_timestamp('$order_end_time2') ".$where."
                  GROUP BY DATE_FORMAT(order_time, '%H');"; 
        }elseif(empty($order_end_time) && !empty($order_start_time)){
            $sql2 ="select DATE_FORMAT(order_time, '%Y-%m-%d %H') time_hour,count(order_id) from db_order
              where DATE_FORMAT(order_time, '%Y-%m-%d') = date_format('$order_start_time', '%Y-%m-%d') ".$where."
              GROUP BY DATE_FORMAT(order_time, '%Y-%m-%d %H');";
        }elseif(!empty($order_end_time) && empty($order_start_time)){
            $sql2 ="select DATE_FORMAT(order_time, '%Y-%m-%d %H') time_hour,count(order_id) from db_order
              where DATE_FORMAT(order_time, '%Y-%m-%d') = date_format('$order_end_time', '%Y-%m-%d') ".$where."
              GROUP BY DATE_FORMAT(order_time, '%Y-%m-%d %H');";
        }elseif(empty($order_end_time) && empty($order_start_time)){
            $sql2 ="select DATE_FORMAT(order_time, '%Y-%m-%d %H') time_hour,count(order_id) from db_order
              where DATE_FORMAT(order_time, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d') ".$where."
              GROUP BY DATE_FORMAT(order_time, '%Y-%m-%d %H');";
        }
        $order = $model->query($sql2);
//        echo $model->getLastSql();
//         dump($order);
        if($order)
        {
            foreach($order as $k => $v)
            {
                $order2[$k] = substr($v["time_hour"],-2);
                $order3[$order2[$k]] = $v["count(order_id)"];          
              }   
            for($i=1;$i<=24;$i++)
           {
                foreach ($order3 as $k => $v) {
                  //static $hour;
                    if($i == $k)
                    {
                      $hour[$i] = (int)$order3[$k]; 
                      break;
                    } else {
                       $hour[$i]=0;
                    }
                }          
           }
          $orders  =  implode ( "," ,  $hour );
          $orders = "[".$orders."]";
//          $this->assign('orders',$orders);
        }else
        {
            $orders ="[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]";
        }
        $formget =array('order_end_time'=>$order_end_time
            ,'order_start_time'=>$order_start_time
            ,'user_end_time'=>$user_end_time
            ,'user_start_time'=>$user_start_time);
        //获取所有大客户
        $UserShop = D('UserShop');
        $this->assign('user_shop',$UserShop->get_user_shop());




        $this->assign('users',$users);
        $this->assign('shop_id',$shop_id);
        $this->assign('orders',$orders);
        $this->assign('formget',$formget);
        
        
        
        
    	$this->assign('server_info', $info);
    	$this->display();






    }
}